#include "robolite.h"

//RoboLite core functions implementation file

#if USE_ANALOG||FULL_CODE
static void adc_init(void)
{
    #warning "adc_init compiled."
    ADCSRA = 0x00; //disable adc
    ADMUX = 0x00;  //select adc input 0
    ACSR  = 0x00;
    ADCSRB = 0x00;
    ADCSRA = 0x81;

}
#endif

#if USE_PWM||FULL_CODE
static void motor_init(void)
{
    #warning "motor_init compiled."
    TCCR0B=0;   //stop timer
    TCCR0A=0b10100011;
/*
Expand like :
    TCCR0A |=(1<<COM0A1);
    TCCR0A |=(1<<COM0B1);
    TCCR0A |=(1<<WGM00);
    TCCR0A |=(1<<WGM01);

*/
    OCR0A=0;
    OCR0B=0;

    TIMSK0=0;   //interrupt off

}
#endif

static void port_init(void)
{
    #warning "port_init compiled."
    PORTB = 0x00;
    DDRB  = 0xFF;
    PORTC = 0x00; //m103 output only
    DDRC  = 0x00;
    PORTD = 0x00;
    DDRD  = 0xFF; //int0 and int1 enabled
}

//call this routine to initialize all peripherals

void rl_init_devices(void)
{
    #warning "rl_init_devices compiled."
    //stop errant interrupts until set up
    CLI(); //disable all interrupts

    port_init();
    
#if USE_ANALOG
    adc_init();
#endif

    MCUCR = 0x00;
    EICRA = 0x00; //extended ext ints

    EIMSK = 0x00;

    TIMSK0 = 0x00; //timer 0 interrupt sources
    TIMSK1 = 0x00; //timer 1 interrupt sources
    TIMSK2 = 0x00; //timer 2 interrupt sources

    PCMSK0 = 0x00; //pin change mask 0
    PCMSK1 = 0x00; //pin change mask 1
    PCMSK2 = 0x00; //pin change mask 2
    PCICR = 0x00; //pin change enable
    PRR = 0x00; //power controller
    SEI(); //re-enable interrupts
//all peripherals are now initialized
#if USE_PWM
    motor_init();
#endif

}
